function [M,SD,N] = meannum(X,id_excl)

%[M,SD,N] = meannum(X)
%
% Let X be a numerical array. This function calculates the mean and standard 
% deviation of numeric elements of X, i.e. the no-NaN,no-Inf, regardless to 
% the structure of X. 
% N is the number of elements of X where the calculation is performed.
% If no elements of X are numeric, M and SD are NaN.
%
%[M,SD,N] = meannum(X,id_excl),
% 
% where id_excl is any value different to 0. In this case, the terms 
% equal to zero are excluded from the calculations.

% G. Teza, 2004, 2007.

if (nargin == 1) || (id_excl == 0) 
    id_excl = false; 
else
    id_excl = true;
end

X = X(:);

In = ~isnan(X);                % search of the "no-NaN" elements
X  = X(In);                          
Ii = ~isinf(X);                % search of the "no-Inf" elements
X  = X(Ii);                          

if id_excl
    J = abs(X) > 100*eps;
    X = X(J);
end

if ~isempty(X)
    M = mean(X); 
    SD = std(X); 
    N = length(X);
else
    M = NaN; 
    SD = NaN; 
    N = 0;
end